//
//  CollectView.m
//  Feel_at_home
//
//  Created by dlios on 15/9/29.
//  Copyright © 2015年 Feel_at_home. All rights reserved.
//

#import "CollectView.h"
#import "WidthAndHeight.pch"
#import "MineModel.h"
#import "CollectTableViewCell.h"
#import "CollectionWebVC.h"
#import "DataBaseHandle.h"
#import "HomeDataViewController.h"

@interface CollectView ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, retain) UIImageView *image;
@property (nonatomic, retain) MineModel *model;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *arr1;

@property (nonatomic, strong) NSMutableArray *cellarr;

@end

@implementation CollectView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"收 藏";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.image = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.image.image = [UIImage imageNamed:@"Back"];
    [self.view addSubview:self.image];
    self.image.alpha = 0.7;
    
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.myTableView.backgroundColor = [UIColor clearColor];
    
    // 判断收藏内是否为空
    [[DataBaseHandle shareDataBase] OpenDB];//打开数据库
    NSMutableArray *arr = [[DataBaseHandle shareDataBase] selectAll];//查找所有数据付给数组
    
    if (arr.count == 0) {
        
        // 判断手机系统版本
        NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
        NSLog(@"手机系统版本: %@", phoneVersion);
        NSLog(@"%ld", (long)phoneVersion.integerValue);
        if (phoneVersion.integerValue >= 8) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您没有任何收藏!\n(^_^)" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }];
            
            [alert addAction:action1];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        } else {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您没有任何收藏!\n(^_^)" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];

        }
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [[DataBaseHandle shareDataBase] OpenDB];
    return [[[DataBaseHandle shareDataBase] selectAll] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    CollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[CollectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    [[DataBaseHandle shareDataBase] OpenDB];
    
    cell.collectionM = [[[DataBaseHandle shareDataBase] selectAll] objectAtIndex:indexPath.row];
  
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeDataViewController *homedata = [[HomeDataViewController alloc] init];

    NSArray *(^block)(NSArray *) = ^(NSArray *arr)
    {
        MineModel *mine = [[MineModel alloc]init];
        mine = [[[DataBaseHandle shareDataBase] selectAll] objectAtIndex:indexPath.row];
        NSString *str1 = mine.title;
        NSString *str2 = mine.url;
        
        arr = [NSMutableArray arrayWithObjects:str1,str2,nil];
        
        return arr;
    };
    homedata.block1 = block;
    
    [self.navigationController pushViewController:homedata animated:YES];
}

// 添加cell的删除方法
// 右侧编辑按钮触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];
}

// 询问那些行需要编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 设置编辑类型,删除,添加
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

// 实现编辑逻辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 删除数据库里表的内容
    [[DataBaseHandle shareDataBase] OpenDB];
    MineModel *collection = [[[DataBaseHandle shareDataBase] selectAll] objectAtIndex:indexPath.row];
    [[DataBaseHandle shareDataBase] deleteCollection:collection.title];
    // 删除cell
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    NSMutableArray *arr = [[DataBaseHandle shareDataBase] selectAll];
    
    if (arr.count == 0) {
        
        // 判断手机系统版本
        NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
        NSLog(@"手机系统版本: %@", phoneVersion);
        NSLog(@"%ld", (long)phoneVersion.integerValue);
        if (phoneVersion.integerValue >= 8) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您的收藏夹以为空!\n(^_^)" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }];
            
            [alert addAction:action1];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        } else {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的收藏夹以为空!\n(^_^)" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
    
        }
    }
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.myTableView reloadData];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
